package cn.itcast.service.impl;

import cn.itcast.constant.MessageConstant;
import cn.itcast.dao.PermissionDao;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Permission;
import cn.itcast.service.PermissionService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Transient;

import java.util.ArrayList;
import java.util.List;

@Service
public class PermissionImpl implements PermissionService {

    @Autowired
    private PermissionDao permissionDao;

    //查询权限管理
    @Override
    public PageResult findAll(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Permission> page = permissionDao.findAll(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    //新增权限管理
    @Override
    public Result add(String name, String keyword, String description) {
        int add = permissionDao.add(name, keyword, description);
        Result result = new Result(true, MessageConstant.ADD_PERMISSION_SUCCESS);
        if (add != 1){
            result.setFlag(false);
            result.setMessage(MessageConstant.ADD_PERMISSION_FAIL);
            return result;
        }else {
            return result;
        }
    }

    //修改权限管理
    @Override
    public Result update(String name, String keyword, String description,Integer id) {
        int update = permissionDao.update(name, keyword, description,id);
        Result result = new Result(true, MessageConstant.EDIT_PERMISSION_SUCCESS);
        if (update != 1){
            result.setFlag(false);
            result.setMessage(MessageConstant.EDIT_PERMISSION_FAIL);
            return result;
        }else {
            return result;
        }
    }

    //弹出编辑框
    @Override
    public Result findById(Integer id) {
        Permission permission = permissionDao.findById(id);
        Result result = new Result(true, MessageConstant.EDIT_PERMISSION_SUCCESS,permission);
        if (permission == null){
            result.setFlag(false);
            result.setMessage(MessageConstant.EDIT_PERMISSION_FAIL);
            return result;
        }else {
            return result;
        }
    }

    //删除权限管理
    @Override
    public Result delete(Integer id) {
        int delete = permissionDao.delete(id);
        if (delete != 1){
            return new Result(false, MessageConstant.DELETE_PERMISSION_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_PERMISSION_SUCCESS);
    }

    @Override
    public List<Permission> findAllPermission() {
        return permissionDao.findAllPermission();
    }


    //角色管理使用根据菜单id查询权限信息
    @Override
    @Transient
    public List<Permission> findByMenuId(Integer[] menuIds) {
        List<Permission> list = new ArrayList<>();
        //根据菜单id查询权限id
        for (Integer menuId : menuIds) {
            Integer[] permissionIds = permissionDao.findByMenuId(menuId);
            if (permissionIds != null){
                //根据权限id查询权限详情
                for (Integer permissionId : permissionIds) {
                    //Permission permission = findById(permissionId);
                    Permission permission= permissionDao.findById(permissionId);
                    //权限详情封装到list
                    list.add(permission);
                }
            }
        }
        return list;
    }


}
